/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * list_games.cc
 * Copyright (C) jurjen 2007 <jurjen@stwerff.xs4all.nl>
 *
 * list_games.cc is free software. You may redistribute it and/or modify it under
 * the terms of the GNU General Public License.
 */

#define DEBUG_ROUTINE "list_games"

#include "list_games.h"
#include "edit_game.h"
#include "show_game.h"
#include "chat_window.h"
#include "moros.h"
#include "../central/game.h"
#include "../general/log.h"


list_games::list_games(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml> refGlade)
: Gtk::Window(cobject), m_refGlade(refGlade)
{
	list_games::show();
	m_refGlade->get_widget("cancel_gamelist", cancel_game);
	m_refGlade->get_widget("new_game", new_game);
	m_refGlade->get_widget("join_game", join_game);
	m_refGlade->get_widget("games_list", games_list);
	m_refGlade->get_widget("general_chat", general_chat);

	games_store = Gtk::ListStore::create(cols);
	load();
	games_list->set_model(games_store);
	games_list->set_search_column(cols.name.index());

	int colnr;
	Gtk::TreeViewColumn* col;
	Gtk::CellRendererText* renderer;

	renderer = Gtk::manage( new Gtk::CellRendererText() );
	colnr = games_list->append_column("Name", *renderer);
	col = games_list->get_column(colnr-1);
	col->add_attribute(renderer->property_text(), cols.name);

	renderer = Gtk::manage( new Gtk::CellRendererText() );
	colnr = games_list->append_column("Owner", *renderer);
	col = games_list->get_column(colnr-1);
	col->add_attribute(renderer->property_text(), cols.owner);

	renderer = Gtk::manage( new Gtk::CellRendererText() );
	colnr = games_list->append_column("Join", *renderer);
	col = games_list->get_column(colnr-1);
	col->add_attribute(renderer->property_text(), cols.join);

	renderer = Gtk::manage( new Gtk::CellRendererText() );
	colnr = games_list->append_column("Goal", *renderer);
	col = games_list->get_column(colnr-1);
	col->add_attribute(renderer->property_text(), cols.goal);

	renderer = Gtk::manage( new Gtk::CellRendererText() );
	colnr = games_list->append_column("Map", *renderer);
	col = games_list->get_column(colnr-1);
	col->add_attribute(renderer->property_text(), cols.game_map);

	renderer = Gtk::manage( new Gtk::CellRendererText() );
	colnr = games_list->append_column("Description", *renderer);
	col = games_list->get_column(colnr-1);
	col->add_attribute(renderer->property_text(), cols.description);

    games_list->signal_row_activated().connect(sigc::mem_fun(*this, &list_games::clicked_game));
	cancel_game->signal_clicked().connect(sigc::mem_fun(*this, &list_games::clicked_cancel));
	new_game->signal_clicked().connect(sigc::mem_fun(*this, &list_games::clicked_new_game));
	general_chat->signal_clicked().connect(sigc::mem_fun(*this, &list_games::clicked_chat));
}

list_games::~list_games() {}

void list_games::load() {
	::map<long, game>::iterator i;
	games_store->clear();
  	for( i=games.begin(); i != games.end(); i++ ) {
		Gtk::TreeRow row = *(games_store->append());
		row[cols.name]=i->second.name;
		row[cols.owner]=i->second.owner;
		row[cols.join]=i->second.join_str();
		row[cols.goal]=i->second.goal_str();
		row[cols.game_map]=i->second.map;
		row[cols.description]=i->second.description;
		row[cols.game_nr]=i->first;
	}
}

void list_games::clicked_cancel() {
	list_games::hide();
}

void list_games::clicked_new_game() {
    dbg_calls("New game");
	edit_game *edit=0;
	m_refGlade->get_widget_derived("edit_game", edit);
	edit->signal_changed.connect(sigc::mem_fun(*this, &list_games::load));
	edit->load(0);
	edit->present();
}

void list_games::clicked_game(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column) {
	Glib::RefPtr<Gtk::TreeSelection> sel=games_list->get_selection();
    Gtk::TreeModel::iterator i=sel->get_selected();
    long gnr=((*i)[cols.game_nr]);
    dbg_calls("Clicked_game %li", gnr);
    game g=get_game(gnr);
    if (g.owner==game_data->user) {
        edit_game *edit=0;
        m_refGlade->get_widget_derived("edit_game", edit);
        edit->signal_changed.connect(sigc::mem_fun(*this, &list_games::load));
        edit->load(gnr);
        edit->present();
    } else {
        show_game *show=0;
        m_refGlade->get_widget_derived("show_game", show);
        show->signal_changed.connect(sigc::mem_fun(*this, &list_games::load));
        show->load(gnr);
        show->present();
    }
}

void list_games::clicked_chat() {
	chat_window *chat=0;
	m_refGlade->get_widget_derived("chat_channel", chat);
	chat->present();
}
